package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {


    @Autowired
    private ApartmentInfoService apartmentInfoService;


    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo) {
        apartmentInfoService.customSaveOrUpdate(apartmentSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("pageItem")
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam long current, @RequestParam long size, ApartmentQueryVo queryVo) {
        Page<ApartmentItemVo> page = new Page<>(current, size);
        apartmentInfoService.customPage(page,queryVo);
        return Result.ok(page);
    }

    @Operation(summary = "根据ID获取公寓详细信息")
    @GetMapping("getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id) {
        /*
        *  实现逻辑:
       根据公寓ID查询公寓详情
       详情设计了多张表
       公寓表
       图片表
       公寓配套相关
       公寓标签相关
       公寓杂费相关
       使用分开查询,进行结果合并的思路! 比直接单条语句查询更加简单!!!
     注意事项:
       1.涉及的多表查询,需要自定义mapper方法
       2.自定义mapper需要注意逻辑删除问题
       3.调用mybatis-plus提供的方法,service层优先! 调用的自定义方法mapper层优化(service除了调用mapper没有其他的业务 [一个业务代码只有单条数据库调用])!!
     实现步骤:
       1. 先根据公寓id查询公寓详情对象
       2. 根据公寓id查询图片的vo对象 (定义mapper方法和mapper方法调用)
       3. 根据公寓id查询配套的对象  (定义mapper方法和mapper方法调用)
       4. 根据公寓id查询标签的对象  (定义mapper方法和mapper方法调用)
       5. 根据公寓id查询杂费的Vo对象 (定义mapper方法和mapper方法调用)
       6. 将数据合并到结果Vo中返回即可
        *
        *
        * */
       ApartmentDetailVo apartmentDetailVo =  apartmentInfoService.customDetailById(id);
        return Result.ok(apartmentDetailVo);
    }

    @Operation(summary = "根据id删除公寓信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        apartmentInfoService.customRemoveById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, @RequestParam ReleaseStatus status) {
//        ApartmentInfo apartmentInfo = new ApartmentInfo();
//        apartmentInfo.setId(id);
//        apartmentInfo.setIsRelease(status);
//        apartmentInfoService.updateById(apartmentInfo);
        LambdaUpdateWrapper<ApartmentInfo> apartmentInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        apartmentInfoLambdaUpdateWrapper.eq(ApartmentInfo::getId,id);
        apartmentInfoLambdaUpdateWrapper.set(ApartmentInfo::getIsRelease,status);
        apartmentInfoService.update(apartmentInfoLambdaUpdateWrapper);
        return Result.ok();
    }

    @Operation(summary = "根据区县id查询公寓信息列表")
    @GetMapping("listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {
        apartmentInfoService.customSelectList(id);
        return Result.ok();
    }
}














